#include "topdbs.hpp"

#include "topdbs_api_c.h"

int topdbs_getClientType(const char* dbsClientName)
{
    return topdbs_client_type(dbsClientName);
}

char* topdbs_getConnError(char* sErrors)
{
    if(!SQLConnection::GetConnection()) return sErrors;
    string errors = SQLConnection::GetConnection()->getErrors();
    strcpy(sErrors,errors.c_str());
    return sErrors;
}

int topdbs_initenv(int dbsType,const char* dbConnStr,const char* user,const char* pass,const char* charset,BOOL encrypt)
{
    eDBSClient_t dbsClient = (eDBSClient_t)dbsType;
    return SQLConnection::InitEnv(dbsClient,dbConnStr,user,pass,charset,encrypt);
}

int topdbs_initenv1(const char* dbsfile,const char* dbsgroup)
{
    return SQLConnection::InitEnv(dbsfile,dbsgroup);
}

int topdbs_uninitenv()
{
    return SQLConnection::UnIntEnv();
}

HCONN topdbs_getconnection()
{
    return SQLConnection::GetConnection();
}

BOOL topdbs_isConnected()
{
    return SQLConnection::GetConnection() ? 
        SQLConnection::GetConnection()->isConnected() : false;
}

int  topdbs_reConnect()
{
    return SQLConnection::GetConnection() ? 
        SQLConnection::GetConnection()->reConnect() : ERR_DBS_FAIL;
}

int  topdbs_disConnect()
{
    return SQLConnection::GetConnection() ? 
         SQLConnection::GetConnection()->disConnect() : ERR_DBS_FAIL;
}

int  topdbs_autoCommit(BOOL on)
{
    return SQLConnection::GetConnection() ? 
         SQLConnection::GetConnection()->autoCommit(on) : ERR_DBS_FAIL;
}

int  topdbs_begin()
{
    return SQLConnection::GetConnection() ? 
         SQLConnection::GetConnection()->begin() : ERR_DBS_FAIL;
}

int  topdbs_commit()
{
    return SQLConnection::GetConnection() ? 
         SQLConnection::GetConnection()->commit() : ERR_DBS_FAIL;
}

int  topdbs_rollback()
{
    return SQLConnection::GetConnection() ? 
         SQLConnection::GetConnection()->rollback() : ERR_DBS_FAIL;
}

BOOL topdbs_isTransaction()
{
    return SQLConnection::GetConnection() ? 
         SQLConnection::GetConnection()->isTransaction() : ERR_DBS_FAIL;
}

HCMD topdbs_newSession()
{
    if ( !SQLConnection::GetConnection() ) return NULL;
    SQLSession* pSession = new SQLSession(SQLConnection::GetConnection());
    return pSession;
}

void topdbs_freeSession(HCMD pSession)
{
    if(pSession)
    {
        SQLSession* Session = (SQLSession*)pSession;
        delete Session;
    }
}

void topdbs_setCommandText(HCMD pSession,const char* sSQL,SQLCommandType_t commandType)
{
     if(pSession)
    {
        SQLSession* Session = (SQLSession*)pSession;
        Session->setCommandText(sSQL,(eDBSCommandType_t)commandType);
    }   
}

BOOL topdbs_isResultSet(HCMD pSession)
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->isResultSet() : false;
}

long topdbs_RowsAffected(HCMD pSession)
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->RowsAffected() : ERR_DBS_FAIL;   
}

BOOL topdbs_IsExecuted(HCMD pSession)
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->IsExecuted() : false;       
}

char* topdbs_GetErrors(HCMD pSession,char* sErrors)
{
    SQLSession* Session = (SQLSession*)pSession;
    string errors =  Session ? Session->getErrors() : "";
    strcpy(sErrors,errors.c_str());
    return sErrors;
}

int topdbs_GetAsString(HCMD pSession,const char* iFiled,char* oValue)
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->GetField(iFiled,oValue) : ERR_DBS_FAIL;
}

int topdbs_GetAsInt(HCMD pSession ,const char* iFiled,int*  oValue)
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->GetField(iFiled,*oValue) : ERR_DBS_FAIL;
}

int topdbs_GetAsLong(HCMD pSession ,const char* iFiled,long* oValue)
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->GetField(iFiled,*oValue) : ERR_DBS_FAIL;
}

void topdbs_Reset(HCMD pSession )
{
    SQLSession* Session = (SQLSession*)pSession;
    if(pSession)  Session->Reset();
}

int  topdbs_Prepare(HCMD pSession )
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->Prepare() : ERR_DBS_FAIL; 
}

//! Executes the current command.
int  topdbs_Execute(HCMD pSession ) 
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->Execute() : ERR_DBS_FAIL; 
}

//! Executes the current command.
int  topdbs_Execute1(HCMD pSession,const char* sSQL) 
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->Execute(sSQL) : ERR_DBS_FAIL; 
}

//! Executes the current command. only fetch one row data.
int  ExecuteFetch(HCMD pSession,const char* sSQL)
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->ExecuteFetch(sSQL) : ERR_DBS_FAIL; 
}

//! Fetches next row from a result set.
BOOL topdbs_FetchNext(HCMD pSession )
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->FetchNext() : false; 
}

//! Fetches next row from a result cursor.
BOOL topdbs_FetchCursorNext(HCMD pSession )
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->FetchCursorNext() : false; 
}

//! Returns the current command params num
int  topdbs_NumsParams(HCMD pSession )
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->NumsParams() : 0; 
}

int  topdbs_BindString(HCMD pSession,const uint16_t paramno,const char* value)
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->BindParams(paramno,value) : ERR_DBS_FAIL; 
}

int  topdbs_BindInt(HCMD pSession,const uint16_t paramno,const int     value)
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->BindParams(paramno,value) : ERR_DBS_FAIL; 
}

int  topdbs_BindLong(HCMD pSession,const uint16_t paramno,const long    value)
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->BindParams(paramno,value) : ERR_DBS_FAIL; 
}

int  topdbs_BindFloat(HCMD pSession,const uint16_t paramno,const float   value)
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->BindParams(paramno,value) : ERR_DBS_FAIL; 
}

int  topdbs_BindDouble(HCMD pSession,const uint16_t paramno,const double  value)
{
    SQLSession* Session = (SQLSession*)pSession;
    return pSession ? Session->BindParams(paramno,value) : ERR_DBS_FAIL; 
}

